<?php

/*
 * Code written by Nguyen Van Hung
 * Feel free to re-use or share it.
 * Happy code!!!
 */

defined('BASEPATH') OR exit('No direct script access allowed');

// This can be removed if you use __autoload() in config.php OR use Modular Extensions
require APPPATH . '/libraries/REST_Controller.php';

class Service extends REST_Controller {
    
    public function __construct() {
        parent:: __construct();
        $this->load->model('service_model');
    }
    
    function user_get(){
        $id = $this->get('id');
        // If $id = null, return all user.
        $response = $this->service_model->getUser($id);
        if ($response){
            $code = 200;
        } else {
            $code = 404;
            $response = "404 -Not found";
        }
        $this->response($response, $code);
    }
    
    function user_level_get(){
        $id = $this->get('id');
        // If $id = null, return all user.
        $response = $this->service_model->getUserLevel($id);
        if ($response){
            $code = 200;
        } else {
            $code = 404;
            $response = "404 -Not found";
        }
        $this->response($response, $code);
    }
    
    // This post method will also insert and update data (because Unity do not support PUT until now)
    function user_level_post(){
        $userId = $this->post('id');
        $levelId = $this->post('level_id');
        $score = $this->post('score');
        $coin = $this->post('coin');
        $time = $this->post('time');
        $star = $this->post('star');
        
        // Check if valid input data
        if ($userId && $levelId){
            $id = $this->service_model->insertUserLevel($userId, $levelId, $score, $coin, $time, $star);
            if ($id){
                $code = 200;
                $response = array('id'=>$userId, 'level_id' => $levelId);
            } else {
                $code = 500;
                $response = "500 - Internal Server Error - Database error";
            }
            $this->response($response, $code);
        } else {
            // Invalid input data
            $code = 400;
            $response = "400 - Bad Request.";
        }
    }
    
    function user_level_put(){
        
        // Get parametter
        $id = $this->put('id');
        $score = $this->put('score');
        $coin = $this->put('coin');
        $levelId = $this->put('level_id');
        $time = $this->put('time');
        $star = $this->put('star');
        if ($levelId && $id){
            if ($this->service_model->updateUserLevel($id, $levelId, $score, $coin, $time, $star)){
                $code = 200;
                $response = array(
                    'id'=>$id,
                    'level_id' => $levelId
                );
            } else {
                $code = 500;
                $response = "500 - Internal Server Error - Database error";
            }
        } else {
            // Invalid input data
            $code = 400;
            $response = "400 - Bad Request. Require 'id' and 'level_id'.";
        }
        $this->response($response, $code);
    }
    
    function quizz_get(){
        $id = $this->get('id');
        if ($id){
            $response = $this->service_model->getQuizz($id, null);
        } else {
            // If no "id" provided.
            // Check if ramdom param?
            $random = $this->get('random');
            if ($random){
                // Filter
                $response = $this->service_model->getQuizz(null, $random);
            } else {
                // Get all quizz
                $response = $this->service_model->getQuizz();
            }
        }
        if ($response){
            $code = 200;
        } else {
            $code = 404;
            $response = "404 -Not found";
        }
        $this->response($response, $code);
    }
    
    
    
    function level_get(){
        $id = $this->get('id');
        // If $id = null, return all user.
        $response = $this->service_model->getLevel($id);
        if ($response){
            $code = 200;
        } else {
            $code = 404;
            $response = "404 -Not found";
        }
        $this->response($response, $code);
    }
    
    function level_delete(){
        $id = $this->get('id');
        if ($this->service_model->deleteLevel($id)){
            $code = 200;
            $response = "Successful";
        } else {
            $code = 404;
            $response = "404 - Not exist.";
        }
        $this->response($response, $code);
    }

}
